#include "SegmentationPanels.h"
#include "VolumeSource.h"


VesselProcessingPanel::VesselProcessingPanel(QWidget *parent):ControlPanelBase(parent)
{
	m_ui.setupUi(this);

	m_panelType = ptVesselSegmentation;
	connect(m_ui.btn_runPV, SIGNAL(clicked()), SLOT(slotPVSegmentation()));
	connect(m_ui.btn_runArtery, SIGNAL(clicked()), SLOT(slotArterySegmentation()));
	connect(m_ui.btn_otsuThresholding, SIGNAL(clicked()), SLOT(slotOtsuThresholding()));
	connect(m_ui.btn_runHessian, SIGNAL(clicked()), SLOT(slotHessianProcessing()));
	connect(m_ui.btn_maxROI, SIGNAL(clicked()), SLOT(slotExtractMaxROI()));
}

VesselProcessingPanel::~VesselProcessingPanel()
{

}

void VesselProcessingPanel::setSource( const QModelIndex& index )
{
	if ( m_isource.isValid())
	{
		m_ui.slider_lower->disconnect(this, SLOT(slotThresholdChanged()));
		m_ui.slider_upper->disconnect(this, SLOT(slotThresholdChanged()));
	}

	ControlPanelBase::setSource(index);
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
		return;

	VolumeSourceBase *vsource = (VolumeSourceBase*)source;

	double range[2];
	source->data()->GetScalarRange(range);

	m_ui.slider_lower->setMinimum(range[0]);
	m_ui.slider_lower->setMaximum(range[1]);
	m_ui.slider_lower->setValue(vsource->getLowerThreshold());
	m_ui.slider_upper->setMinimum(range[0]);
	m_ui.slider_upper->setMaximum(range[1]);
	m_ui.slider_upper->setValue(vsource->getUpperThreshold());

	connect(m_ui.slider_lower, SIGNAL(valueChanged(int)), SLOT(slotThresholdChanged()));
	connect(m_ui.slider_upper, SIGNAL(valueChanged(int)), SLOT(slotThresholdChanged()));
}


void VesselProcessingPanel::slotThresholdChanged()
{
	((VolumeSourceBase*)currentSource())->setThresholdBetween(m_ui.slider_lower->value(), m_ui.slider_upper->value());
}

void VesselProcessingPanel::slotHessianProcessing()
{
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
	{
		qDebug() << "[Vessel]: Source is not a Volume";
		return;
	}

	switch(source->dataType())
	{
	case VTK_SHORT:
		{
			createHessianThread<short>();
			break;
		}
	case VTK_FLOAT:
		{
			createHessianThread<float>();
			break;
		}
	default:
		{
			qDebug("[Vessel]: doesn't support current data type [%d]", source->dataType());
			break;
		}
	}
}

void VesselProcessingPanel::slotOtsuThresholding()
{
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
	{
		qDebug() << "[Vessel]: Source is not a Volume";
		return;
	}

	switch(source->dataType())
	{
	case VTK_UNSIGNED_CHAR:
		{
			createOtsuThread<unsigned char>();
			break;
		}
	case VTK_SHORT:
		{
			createOtsuThread<short>();
			break;
		}
	case VTK_FLOAT:
		{
			createOtsuThread<float>();
			break;
		}
	default:
		{
			qDebug("[Vessel]: doesn't support current data type [%d]", source->dataType());
			break;
		}
	}
}

void VesselProcessingPanel::slotPVSegmentation()
{
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
	{
		qDebug() << "[Vessel]: Source is not a Volume";
		return;
	}

	if ( m_displayModel->currentSeed().isEmpty())
	{
		qDebug() << "[Vessel]: Add a seed first";
		return;
	}

	switch(source->dataType())
	{
	case VTK_UNSIGNED_CHAR:
		{
			createCTThread<unsigned char>();
			break;
		}
	case VTK_SHORT:
		{
			createCTThread<short>();
			break;
		}
	case VTK_FLOAT:
		{
			createCTThread<float>();
			break;
		}
	default:
		{
			qDebug("[Vessel]: doesn't support current data type [%d]", source->dataType());
			break;
		}
	}
}

void VesselProcessingPanel::slotArterySegmentation()
{
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
	{
		qDebug() << "[Vessel]: Source is not a Volume";
		return;
	}

	if ( m_displayModel->currentSeed().isEmpty())
	{
		qDebug() << "[Vessel]: Add a seed first";
		return;
	}

	switch(source->dataType())
	{
	case VTK_UNSIGNED_CHAR:
		{
			createCCThread<unsigned char>();
			break;
		}
	case VTK_SHORT:
		{
			createCCThread<short>();
			break;
		}
	case VTK_FLOAT:
		{
			createCCThread<float>();
			break;
		}
	default:
		{
			qDebug("[Vessel]: Doesn't support current data type [%d]", source->dataType());
			break;
		}
	}
}

bool VesselProcessingPanel::slotExtractMaxROI()
{
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
	{
		qDebug() << "[Vessel]: Source is not a Volume";
		return false;
	}

	switch(source->dataType())
	{
	case VTK_UNSIGNED_CHAR:
		{
			createThread_ExtractMAXROI<unsigned char>();
			break;
		}
	case VTK_SHORT:
		{
			createThread_ExtractMAXROI<short>();
			break;
		}
	case VTK_FLOAT:
		{
			createThread_ExtractMAXROI<float>();
			break;
		}
	default:
		{
			qDebug("[Vessel]: Doesn't support current data type [%d]", source->dataType());
			break;
		}
	}

	return true;
}




LiverProcessingPanel::LiverProcessingPanel(QWidget *parent):ControlPanelBase(parent)
{
	m_ui.setupUi(this);
	m_ui.groupBox->hide();
	m_panelType = ptLiverSegmentation;
	connect(m_ui.btn_run, SIGNAL(clicked()), SLOT(run()));
}


LiverProcessingPanel::~LiverProcessingPanel()
{

}

void LiverProcessingPanel::setDisplayModel(DisplayModel* dm)
{
	ControlPanelBase::setDisplayModel(dm);
}

void LiverProcessingPanel::setSource(const QModelIndex& index)
{
	if ( m_isource.isValid())
	{
		m_ui.slider_lower->disconnect(this, SLOT(slotThresholdChanged()));
		m_ui.slider_upper->disconnect(this, SLOT(slotThresholdChanged()));
	}

	ControlPanelBase::setSource(index);
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
		return;

	VolumeSourceBase *vsource = (VolumeSourceBase*)source;


	double range[2];
	int extent[6];
	vsource->data()->GetScalarRange(range);
	vsource->data()->GetExtent(extent);

	m_ui.slider_lower->setMinimum(range[0]);
	m_ui.slider_lower->setMaximum(range[1]);
	m_ui.slider_lower->setValue(vsource->getLowerThreshold());
	m_ui.slider_upper->setMinimum(range[0]);
	m_ui.slider_upper->setMaximum(range[1]);
	m_ui.slider_upper->setValue(vsource->getUpperThreshold());

	connect(m_ui.slider_lower, SIGNAL(valueChanged(int)), SLOT(slotThresholdChanged()));
	connect(m_ui.slider_upper, SIGNAL(valueChanged(int)), SLOT(slotThresholdChanged()));

	m_ui.spin_toRight->setMaximum(extent[5]);
	m_ui.spin_toRight->setValue(extent[5]);
	m_ui.spin_toLeft->setMaximum(extent[5]);
}


void LiverProcessingPanel::run()
{
	Source *source = currentSource();

	if ( !source || source->type() != stVolume)
	{
		qDebug() << "[Liver]: Source is not a Volume";
		return;
	}

	if ( m_displayModel->currentSeed().isEmpty())
	{
		qDebug() << "[Liver]: Add a seed first";
		return;
	}

	switch(currentSource()->dataType())
	{
	case VTK_UNSIGNED_CHAR:
		{
			createThreads<unsigned char>();
			break;
		}
	case VTK_SHORT:
		{
			createThreads<short>();
			break;
		}
	case VTK_FLOAT:
		{
			createThreads<float>();
			break;
		}
	default:
		{
			qDebug() << "[Warning]: Doesn't support data type " << currentSource()->dataType();
			break;
		}
	}
}

void LiverProcessingPanel::slotThresholdChanged()
{
	((VolumeSourceBase*)currentSource())->setThresholdBetween(m_ui.slider_lower->value(), m_ui.slider_upper->value());
}




